<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/22
 * Time: 21:18
 */

namespace Home\Controller;


use Home\Model\ClassTaskModel;
use Think\Controller;

class UserCenterController extends Controller
{
    public function center(){
        $userInfo = D('User')->where(array('id'=>$_SESSION['lblUser']['id']))->find();
        if($userInfo['status']==0){
            redirect(U('Index/home'));
        }
        $this->assign('userInfo',$userInfo);
        $this->display('Index/personal');
    }
    public function index(){
        $userInfo = D('User')->where(array('id'=>$_SESSION['lblUser']['id']))->find();
        if($userInfo['status']==0){
            redirect(U('Index/home'));
        }
        $this->assign('userInfo',$userInfo);
       $this->assign('class',M('class')->select());
        $this->display('Index/modify');
    }

    /**
     * 我购买的课程
     */
    public function myCourses(){
        $myCourseList = M('order')->where(array('user_id'=>$_SESSION['lblUser']['id'],'status'=>2))->select();
        if ($myCourseList){
            foreach ($myCourseList as $key=>$val){
                $courseInfo = M('courses')->where(array("id"=>$val['courses_id']))->find();
                $myCourseList[$key]['courseInfo'] = $courseInfo;
                $myCourseList[$key]['courseInfo']['value'] = substr_replace(strip_tags(html_entity_decode($courseInfo['value'])),"...",30);
            }
        }

        $this->assign('lists',$myCourseList);
        $this->display('Index/curriculum_list');
    }
    public function task(){
        $cid = I('get.id');
        $map['course_id'] = $cid;
        $map['class_date'] = array('GT',date('Y-m-d',time()));
        $nowTask = M('class_task')->where($map)->find();//即将签到课程

        $signLog = M('class_signup')->where(array('user_id'=>$_SESSION['lblUser']['id'],'class_id'=>$nowTask['id']))->find();//签到日志
        if (!$signLog){
            $data['msg'] = '你还未签到，也未请假，逾期未操作算旷课哦！';
        }
        if ($signLog && $signLog['is_sign']== '1'){
            $data['msg'] = '你已经签过到了！';
        }
        if ($signLog && $signLog['is_leave'] == '1'){
            $data['msg'] = '你已经请过假了！如需取消请假请直接签到。。。';
        }

        $this->assign('msg',$data);
        $this->assign('classInfo',$nowTask);
        $this->assign('signLog',$signLog);
        $this->display('Index/task');
    }

    /**
     * 签到请假
     */
    public function ajaxSignup(){
        $id = I('post.id');
        $type = I('post.type');

        $signLog = M('class_signup')->where(array('user_id'=>$_SESSION['lblUser']['id'],'class_id'=>$id))->find();//签到日志

        if (!$signLog){
            $data['class_id'] = $id;
            $data['user_id'] = $_SESSION['lblUser']['id'];
            $data['createtime'] = time();
            if ($type == 'signup'){
                $data['is_sign'] = '1';
                $data['is_leave'] = '0';
                $insertId = M('class_signup')->add($data);
                if ($insertId){
                    $data['msg'] = '签到成功';
                    $data['code'] = 200;
                    $this->ajaxRuturn($data);
                }else{
                    $data['msg'] = '签到失败';
                    $data['code'] = 400;
                    exit(json_encode($data));
                }
            }else{
                $data['is_sign'] = '0';
                $data['is_leave'] = '1';
                $insertId = M('class_signup')->add($data);
                if ($insertId){
                    $data['msg'] = '请假成功';
                    $data['code'] = 200;
                    exit(json_encode($data));
                }else{
                    $data['msg'] = '请假失败';
                    $data['code'] = 400;
                    exit(json_encode($data));
                }
            }
        }else{
            if ($type == 'signup'){
                if ($signLog && $signLog['is_sign']== '1'){
                    $data['msg'] = '你已经签过到了！';
                    $data['code'] = 400;
                    exit(json_encode($data));
                }
                if ($signLog && $signLog['is_leave'] == '1'){
                    $data['id'] = $signLog['id'];
                    $data['is_sign'] = '1';
                    $data['is_leave'] = '0';
                    $data['createtime'] = time();
                    if (M('class_signup')->save($data) !==false){
                        $data['msg'] = '你已将请假改为签到，请准时上课！';
                        $data['code'] = 200;
                        exit(json_encode($data));
                    }else{
                        $data['msg'] = '改签到失败';
                        $data['code'] = 400;
                        exit(json_encode($data));
                    }
                }
            }else{
                if ($signLog && $signLog['is_leave']== '1'){
                    $data['msg'] = '你已经请过假了！';
                    $data['code'] = 400;
                    exit(json_encode($data));
                }
                if ($signLog && $signLog['is_sign'] == '1'){
                    $data['id'] = $signLog['id'];
                    $data['is_sign'] = '0';
                    $data['is_leave'] = '1';
                    $data['createtime'] = time();
                    if (M('class_signup')->save($data) !==false){
                        $data['msg'] = '你已将签到改为请假！';
                        $data['code'] = 200;
                        exit(json_encode($data));
                    }else{
                        $data['msg'] = '改请假失败';
                        $data['code'] = 400;
                        exit(json_encode($data));
                    }
                }
            }
        }
    }

    //用户报名
    public  function update(){

        if($_POST){
            $data = I('post.');
            if(D('User')->where(array('id'=>$_SESSION['lblUser']['id']))->save($data)){

                $msg['status'] =1;
                $msg['error'] = '修改成功';
                $this->ajaxReturn($msg);
            }else{
                $msg['error'] = '修改失败';
                $this->ajaxReturn($msg);
            }


        }
    }

    //士兵证
    public function Soldbuch(){
        $order_id = I('get.course_id');
        $userInfo = D('User')->where(array('id'=>$_SESSION['lblUser']['id']))->find();
        if($userInfo['status']==0){
            redirect(U('Index/home'));
        }
        $this->assign('userInfo',$userInfo);
        $this->display('Index/Soldbuch');
    }
}